﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Common;
using CharityGamesOnline.Modules.Event;

namespace CharityGamesOnline.Modules.Game
{
    public partial class Game : CGOBase, IModuleListener
    {
        private const string DEFAULT_CONTROL = "GameEnrollmentList.ascx";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (ModuleConfiguration.ModuleControl.SupportsPartialRendering)
                {
                    UpdatePanel upContainer = this.Parent.Parent as UpdatePanel;
                    if (upContainer != null)
                    {
                        upContainer.UpdateMode = UpdatePanelUpdateMode.Always;
                    }
                }

                LoadModuleControl();
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #region IModuleListener Members

        public void OnModuleCommunication(object s, ModuleCommunicationEventArgs e)
        {
            if (ModuleConfiguration.ModuleControl.SupportsPartialRendering)
            {
                switch (e.Sender)
                {                    
                    case "CharityGamesOnline.Modules.Event.EventNav":
                        HandleEventCommunicastion(e);
                        break;
                }
            }
        }
        #endregion

        #region Private Function

        private void LoadModuleControl()
        {
            string strControlName = DEFAULT_CONTROL;

            if (string.IsNullOrEmpty(Request.QueryString["mctl"]))
            {
                if (!Null.IsNull(Settings["ViewMode"]))
                {
                    strControlName = (string)Settings["ViewMode"];
                }
            }
            else
            {
                strControlName = Request.QueryString["mctl"];
                if (!strControlName.EndsWith(".ascx"))
                    strControlName += ".ascx";
            }

            switch (strControlName.ToLower())
            {
                case "gamedetail.ascx":
                case "leaderboard.ascx":
                case "gameenrollmentlist.ascx":
                    LodaControl(strControlName);
                    break;
            }
        }

        private void LodaControl(string strControlToLoad)
        {
            PortalModuleBase objModule = LoadControl(strControlToLoad) as PortalModuleBase;
            objModule.ModuleConfiguration = ModuleConfiguration.Clone();
            objModule.ID = System.IO.Path.GetFileNameWithoutExtension(strControlToLoad);
            phMain.Controls.Add(objModule);
        }        

        private void HandleEventCommunicastion(ModuleCommunicationEventArgs e)
        {
            switch (e.Type)
            {
                case EventNav.COMM_TYPE:
                    LeaderBoard objLeaderBoard = FindControl<LeaderBoard>(phMain);
                    if (!Null.IsNull(objLeaderBoard))
                    {
                        objLeaderBoard.BindData(Int32.Parse((string)e.Value));
                    }
                    break;
            }
        }
        #endregion
    }
}